<template>
  <div>
    <div class="header_search_nav search_form">
      <div class="ipt_left">
        <el-form ref="queryForm" :model="queryParams" label-width="150px">
          <el-row>
            <el-col :span="6">
              <el-form-item label="门店" prop="storeId">
                <el-select
                  v-model="queryParams.storeId"
                  placeholder="请选择"
                  filterable
                  @change="getList"
                >
                  <el-option
                    v-for="(item, index) in $store.state.user.stores"
                    :key="index"
                    :label="item.name"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label="采购状态" prop="buyStatus">
                <el-select
                  v-model="queryParams.buyStatus"
                  placeholder="请选择"
                  class="w-200"
                >
                  <el-option
                    v-for="item in buyStatusData"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <div class="timebox">
                <el-form-item label="日期时间" prop="date">
                  <timer
                    ref="timer"
                    :timer.sync="queryParams.date"
                    @query="handleQuery"
                    isCheck="false"
                  ></timer>
                </el-form-item>
              </div>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <div class="btn_right">
        <button class="btn1" @click="handleQuery">筛选</button>
        <button class="btn2" @click="resetQuery">重置</button>
      </div>
    </div>

    <!-- 所有列表 -->
    <div class="table_card">
      <!-- 上部分 title -->
      <div class="table_card_top">
        <span class="solid_left"></span>
        <div class="table_card_title">门店采购</div>
        <div class="op_right">
          <button class="primary_button" @click="ApprovalBatch">
            批量审批
          </button>
          <button class="primary_button" @click="shipmentsBatch">
            批量发货
          </button>
          <button class="primary_button" @click="exportStorePurchas">
            导出
          </button>
        </div>
      </div>

      <div class="table_list">
        <el-table
          :data="tableList"
          stripe
          style="width: 100%"
          v-loading="loading"
          @selection-change="handleSelectionChange"
          row-key="buyId"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column prop="storeName" label="门店名称" width="130">
          </el-table-column>
          <el-table-column
            prop="storeContentName"
            label="采购内容名称"
            width="130"
          >
          </el-table-column>
          <el-table-column
            prop="storeContentUnit"
            label="采购内容单位"
            width="100"
          >
          </el-table-column>
          <el-table-column
            prop="storeContentDesc"
            label="采购内容描述"
            width="170"
          >
          </el-table-column>
          <el-table-column prop="storeAddressLink" label="联系人">
          </el-table-column>
          <el-table-column
            prop="storeAddressMobile"
            label="联系电话"
            width="100"
          >
          </el-table-column>
          <el-table-column prop="customerCount" label="客量"> </el-table-column>
          <el-table-column prop="storePo" label="业绩"> </el-table-column>
          <el-table-column prop="createName" label="申请人"> </el-table-column>
          <el-table-column prop="createTime" label="申请时间" width="130">
          </el-table-column>
          <el-table-column
            prop="buySendUserName"
            label="发货人名称"
            width="100"
          >
          </el-table-column>
          <el-table-column prop="storeAddress" label="收货地址" width="130">
          </el-table-column>
          <el-table-column prop="buyContentCount" label="采购数量">
          </el-table-column>
          <el-table-column prop="buySendTime" label="发货时间">
          </el-table-column>
          <el-table-column prop="buyStatus" label="状态">
            <template slot-scope="scope">
              <div>{{ buyStatusObj[scope.row.buyStatus] }}</div>
            </template>
          </el-table-column>
          <el-table-column prop="buyCollectTime" label="收货时间">
          </el-table-column>
          <el-table-column prop="checkUserName" label="审核人">
          </el-table-column>
          <el-table-column prop="checkTime" label="审核时间"> </el-table-column>
          <el-table-column prop="checkDesc" label="审核备注"> </el-table-column>
          <el-table-column label="操作" width="130px" fixed="right">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="Approval(scope.row)"
                v-if="scope.row.buyStatus === 'A'"
                >审批</el-button
              >
              <el-button
                size="mini"
                type="text"
                @click="shipments(scope.row)"
                v-if="scope.row.buyStatus === 'W'"
                >发货</el-button
              >
              <el-button size="mini" type="text" @click="CheckInfo(scope.row)"
                >详情</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <pagination
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="getLists"
        />
      </div>

      <el-dialog
        title="审核"
        :visible.sync="storePurchasingOpen"
        width="30%"
        :before-close="CloseStorePurchasing"
      >
        <span>
          <div>
            <el-form
              ref="storePurchasingRef"
              :rules="storePurchasingRules"
              :model="storePurchasingForm"
            >
              <el-row :gutter="10">
                <el-col :span="8"
                  ><div>
                    <el-form-item label="审核状态" prop="checkStatus">
                      <div class="checkBox">
                        <el-radio
                          v-model="storePurchasingForm.checkStatus"
                          label="Y"
                          >审核通过</el-radio
                        >
                        <el-radio
                          v-model="storePurchasingForm.checkStatus"
                          label="N"
                          >审核拒绝</el-radio
                        >
                      </div>
                    </el-form-item>
                  </div>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col>
                  <el-form-item label="审核备注" prop="checkDesc">
                    <el-input
                      type="textarea"
                      v-model="storePurchasingForm.checkDesc"
                      clearable
                    />
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </div>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="CloseStorePurchasing">取 消</el-button>
          <el-button type="primary" @click="storePurchasingSubmit"
            >确 定</el-button
          >
        </span>
      </el-dialog>

      <!-- 详情 -->
      <el-dialog
        title="采购详情"
        :visible.sync="storePurchasingModel"
        width="30%"
        :before-close="CloseStorePurchasingInfo"
      >
        <span>
          <div>
            <el-form
              ref="storePurchasingInfoRef"
              :model="storePurchasingInfoForm"
            >
              <el-row :gutter="10">
                <el-col>
                  <el-form-item label="联系人" prop="storeAddressLink">
                    <el-input
                      class="w-100"
                      type="text"
                      v-model="storePurchasingInfoForm.storeAddressLink"
                      disabled
                    ></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col>
                  <el-form-item label="联系电话" prop="storeAddressMobile">
                    <el-input
                      class="w-100"
                      type="text"
                      v-model="storePurchasingInfoForm.storeAddressMobile"
                      disabled
                    ></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col>
                  <el-form-item label="申请时间" prop="createTime">
                    <el-date-picker
                      v-model="storePurchasingInfoForm.createTime"
                      type="datetime"
                      disabled
                      class="w-100"
                    >
                    </el-date-picker>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col>
                  <el-form-item label="发货人名称" prop="buySendUserName">
                    <el-input
                      class="w-100"
                      type="text"
                      v-model="storePurchasingInfoForm.buySendUserName"
                      disabled
                    ></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col>
                  <el-form-item label="收货地址" prop="storeAddress">
                    <el-input
                      class="w-100"
                      type="text"
                      v-model="storePurchasingInfoForm.storeAddress"
                      disabled
                    ></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col>
                  <el-form-item label="发货时间" prop="buySendTime">
                    <el-date-picker
                      v-model="storePurchasingInfoForm.buySendTime"
                      type="datetime"
                      disabled
                      class="w-100"
                    >
                    </el-date-picker>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col>
                  <el-form-item label="收货时间" prop="buyCollectTime">
                    <el-date-picker
                      v-model="storePurchasingInfoForm.buyCollectTime"
                      type="datetime"
                      disabled
                      class="w-100"
                    >
                    </el-date-picker>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col>
                  <el-form-item label="审核人" prop="checkUserName">
                    <el-input
                      class="w-100"
                      type="text"
                      v-model="storePurchasingInfoForm.checkUserName"
                      disabled
                    ></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col>
                  <el-form-item label="审核时间" prop="checkTime">
                    <el-date-picker
                      v-model="storePurchasingInfoForm.checkTime"
                      type="datetime"
                      disabled
                      class="w-100"
                    >
                    </el-date-picker>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="10">
                <el-col>
                  <el-form-item label="审核备注" prop="checkDesc">
                    <el-input
                      class="w-100"
                      type="text"
                      v-model="storePurchasingInfoForm.checkDesc"
                      disabled
                    ></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </div>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="CloseStorePurchasingInfo">取 消</el-button>
          <el-button type="primary" @click="storePurchasingInfoSubmit"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  getStoreBuyPage,
  sendbuy,
  Checkbuy,
  StoreBuyExport,
  getStoreBuyInfo,
} from "@/api/business/storePurchasing";
import { mapGetters } from "vuex";
import ClientInfo from "@/components/ClientInfo/clientInfo.vue";
import timer from "@/components/getTime/timer";

export default {
  name: "Storeadminclientlist",
  components: {
    ClientInfo,
    timer,
  },
  data() {
    return {
      queryParams: {
        storeId: "",
        pageNo: 1,
        pageSize: 10,
        buyStatus: "", // 采购状态(A:待审批;W:待发货;S:待收货;F:已收货;N:审核拒绝)
        date: [],
        createStartTime: "", // 采购申请开始时间
        createEndTime: "", // 采购申请结束时间
      },
      total: 0,
      tableList: [],

      // 采购状态(A:待审批;W:待发货;S:待收货;F:已收货;N:审核拒绝)
      buyStatusObj: {
        A: "待审批",
        W: "待发货",
        S: "待收货",
        F: "已收货",
        N: "审核拒绝",
      },

      buyStatusData: [
        {
          value: "A",
          label: "待审批",
        },
        {
          value: "W",
          label: "待发货",
        },
        {
          value: "S",
          label: "待收货",
        },
        {
          value: "F",
          label: "已收货",
        },
        {
          value: "N",
          label: "审核拒绝",
        },
      ],

      buyIdList: [],
      multipleSelection: [],

      storePurchasingOpen: false,
      storePurchasingForm: {
        buyIdList: [], // 采购标识集合
        checkDesc: "", // 审核备注
        checkStatus: "Y", // 审核状态:Y:审核通过;N:审核拒绝
      },

      storePurchasingRules: {
        checkStatus: [
          { required: true, message: "请选择审核状态", trigger: "change" },
        ],
      },

      storePurchasingModel: false, // 详情
      storePurchasingInfoForm: {
        storeAddressLink: "",
        storeAddressMobile: "",
        createTime: "",
        buySendUserName: "",
        storeAddress: "",
        buyCollectTime: "",
        buySendTime: "",
        checkUserName: "",
        checkTime: "",
        checkDesc: "",
      },

      StoreBuyInfoData: {}, // 详情
    };
  },

  computed: {
    ...mapGetters(["userInfo"]),
  },
  created() {
    this.getLists();
  },

  mounted() {
    document.addEventListener("paste", function (e) {
      if (e.target.offsetParent.classList.contains("pwd")) {
        e.preventDefault();
      }
    });
  },

  methods: {
    storePurchasingInfoSubmit() {
      this.storePurchasingModel = false;
    },

    CloseStorePurchasingInfo() {
      this.storePurchasingModel = false;
    },
    // 详情
    CheckInfo(row) {
      this.storePurchasingModel = true;
      console.log("详情", row);
      this.getStoreBuyInfoData(row);
    },

    getStoreBuyInfoData(row) {
      getStoreBuyInfo({
        buyId: row.buyId,
      })
        .then((res) => {
          console.log(res, "详情");
          // this.StoreBuyInfoData = res;
          this.storePurchasingInfoForm.storeAddressLink = row.storeAddressLink;
          this.storePurchasingInfoForm.storeAddressMobile =
            row.storeAddressMobile;
          this.storePurchasingInfoForm.createTime = row.createTime;
          this.storePurchasingInfoForm.buySendUserName = row.buySendUserName;
          this.storePurchasingInfoForm.storeAddress = row.storeAddress;
          this.storePurchasingInfoForm.buyCollectTime = row.buyCollectTime;
          this.storePurchasingInfoForm.buySendTime = row.buySendTime;
          this.storePurchasingInfoForm.checkUserName = row.checkUserName;
          this.storePurchasingInfoForm.checkTime = row.checkTime;
          this.storePurchasingInfoForm.checkDesc = row.checkDesc;
        })
        .catch((err) => {});
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log("val", val);

      // console.log("multipleSelection", this.multipleSelection);

      let selectIdList = [];

      for (let index = 0; index < val.length; index++) {
        const element = val[index];
        selectIdList.push(element.buyId);
      }

      this.storePurchasingForm.buyIdList = selectIdList;

      console.log(this.buyIdList, "选中的数据");
    },

    // 批量审批
    ApprovalBatch() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          type: "error",
          message: "请先勾选需要批量审批的数据",
        });
        return false;
      }

      this.storePurchasingOpen = true;
    },

    // 单个审批
    Approval(row) {
      this.storePurchasingOpen = true;
      this.storePurchasingForm.buyIdList = [row.buyId];
    },

    // 导出
    exportStorePurchas() {
      this.$modal
        .confirm("是否确认导出门店采购数据?")
        .then(() => {
          this.exportLoading = true;
          let queryParams = JSON.parse(JSON.stringify(this.queryParams));

          if (!!this.queryParams.date) {
            queryParams.createStartTime = this.queryParams.date[0];
            queryParams.createEndTime = this.queryParams.date[1];
          }
          delete queryParams.date;
          return StoreBuyExport(queryParams);
        })
        .then((response) => {
          this.$download.excel(response, "门店采购");
          this.exportLoading = false;
        })
        .catch(() => {
          this.exportLoading = false;
        });
    },

    // 批量发货
    shipmentsBatch() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          type: "error",
          message: "请先勾选需要批量发货的数据",
        });
        return false;
      } else {
        this.$confirm("确定要批量发货吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            sendbuy({
              buyIdList: this.storePurchasingForm.buyIdList,
            })
              .then((res) => {
                const { code } = res;
                if (code === 0) {
                  this.getLists();
                  this.$message({
                    type: "success",
                    message: "操作成功!",
                  });
                }
              })
              .catch(() => {});
          })
          .catch(() => {});
      }
    },

    // 单个发货
    shipments(row) {
      this.storePurchasingForm.buyIdList = [row.buyId];
      this.$confirm("确定要发货吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          sendbuy({
            buyIdList: this.storePurchasingForm.buyIdList,
          })
            .then((res) => {
              const { code } = res;
              if (code === 0) {
                this.getLists();
                this.$message({
                  type: "success",
                  message: "操作成功!",
                });
              }
            })
            .catch(() => {});
        })
        .catch(() => {});
    },

    CloseStorePurchasing() {
      this.resetFormData("storePurchasingRef");

      this.$nextTick(() => {
        this.storePurchasingOpen = false;
      });
    },
    storePurchasingSubmit() {
      this.$refs["storePurchasingRef"].validate((valid) => {
        Checkbuy(this.storePurchasingForm)
          .then((Res) => {
            const { code } = Res;
            if (code === 0) {
              this.$message({
                type: "success",
                message: "操作成功",
              });
              this.getLists();
              this.CloseStorePurchasing();
            }
          })
          .catch((err) => {});
      });
    },

    getLists() {
      this.loading = true;
      const query = JSON.parse(JSON.stringify(this.queryParams));

      if (!!query.date) {
        query.createStartTime = this.queryParams.date[0];
        query.createEndTime = this.queryParams.date[1];
      }
      delete query.date;
      getStoreBuyPage(query)
        .then((res) => {
          console.log(res, "门店采购分页");
          this.loading = false;
          const { code, data } = res;
          if (code === 0) {
            this.tableList = data.list;
            this.total = data.total;
          }
        })
        .catch((err) => {
          this.loading = false;
        });
    },

    // 筛选
    handleQuery() {
      this.getLists();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetFormData("queryForm");
      this.handleQuery();
      this.getLists();
    },

    resetFormData(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
.pick-clien-style {
  .pick-client-head {
    display: flex;
    justify-content: space-between;
    margin-bottom: 20px;
    .input-left {
      width: 300px;
      position: relative;
      padding-right: 80px;
      .but {
        position: absolute;
        top: 0px;
        right: 0px;
      }
    }
  }

  .table-head {
    padding-left: 80px;
    height: 48px;
    background-color: #f7f8fa;
    line-height: 48px;
    font-size: 12px;
  }

  .pick-item-list {
    max-height: 500px;
    overflow-y: auto;
    .pick-item-style {
      border-bottom: solid 1px #f2f2f2;
      padding: 20px 0px 20px 36px;
    }
    .pick-item-center {
      width: 100%;
      display: flex;
      align-items: center;

      .user-style {
        font-size: 14px;

        .user-name {
          color: #3b98fe;
        }

        .span_small {
          font-size: 12px;
          color: #999999;
          margin-top: 8px;
        }
      }
    }
  }
}

.f-zero {
  font-size: 0px;
}
.table_list {
  .clients {
    cursor: pointer;
  }
}

.switch-style {
  display: inline-block;
  position: relative;
  &::before {
    position: absolute;
    width: 100%;
    content: "";
    height: 100%;
    z-index: 2;
    cursor: pointer;
  }
}

.client_name {
  color: #1890ff;
}

.checkBox {
  display: flex;
  margin-top: 12px;
}
</style>
